package com.linesum.code.controller.restfulapi;

import java.util.Collections;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.linesum.code.commons.ResultResponse;
import com.linesum.code.dto.UserDto;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * restful api 
 * 访问路径 localhost:8080/swagger-ui.html
 * @author chenzf
 * @date 2017年7月26日
 */
@RestController
@RequestMapping("/restfulApi")
public class RestfulApiController {
	private Map<String, UserDto> userMap = Collections.synchronizedMap(Maps.newHashMap());

	@ApiOperation(value = "获取用户列表")
	@GetMapping(value = "userList", produces = "application/json; charset=UTF-8")
	public List<UserDto> getAllUser() {
		return Lists.newArrayList(userMap.values());
	}

	//展示请求参数并指定请求参数类型  dataType = "UserDto"
	@ApiOperation(value = "添加用户", notes = "根据userdto对象添加新的对象")
	@ApiImplicitParam(name = "userDto", value = "用户信息dto", required = true, dataType = "UserDto")
	@PostMapping(value = "addUser", produces = "application/json; charset=UTF-8")
	public ResultResponse addUser(@RequestBody UserDto userDto) {
		if (userDto != null) {
			userMap.put(userDto.getId().toString(), userDto);
			return new ResultResponse(true);
		}
		return new ResultResponse(false, "数据为空");
	}

	@ApiOperation(value = "获取用户详情", notes = "根据用户id查询用户信息")
	@ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "String")
	@GetMapping(value = "userDetail/{id}", produces = "application/json; charset=UTF-8")
	public UserDto detail(@PathVariable String id) {
		if (StringUtils.isBlank(id)) {
			return null;
		}
		return userMap.get(id);
	}

	@ApiOperation(value = "更新用户信息", notes = "根据请求的最新用户信息更新对应的用户信息")
	@ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "String"),
			@ApiImplicitParam(name = "userDto", value = "用户信息", required = true, dataType = "UserDto") })
	@PutMapping(value = "updateUser/{id}", produces = "application/json; charset=UTF-8")
	public ResultResponse updateUser(@PathVariable String id, @RequestBody UserDto userDto) {
		if (StringUtils.isBlank(id) || userDto == null) {
			return new ResultResponse(false, "数据为空");
		}
		UserDto oldUser = userMap.get(id);
		if (oldUser == null) {
			return new ResultResponse(false, "查无数据");
		}
		BeanUtils.copyProperties(userDto, oldUser);
		userMap.put(id, oldUser);
		return new ResultResponse(true);
	}

	@ApiOperation(value = "删除用户信息", notes = "根据请求用户id删除对应的用户信息")
	@ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "String")
	@DeleteMapping(value = "deleteUser/{id}", produces = "application/json; charset=UTF-8")
	public ResultResponse deleteUser(@PathVariable String id) {
		if (StringUtils.isBlank(id)) {
			return new ResultResponse(false, "数据为空");
		}
		if (userMap.get(id) == null) {
			return new ResultResponse(false, "无此数据，无需删除");
		}
		userMap.remove(id);
		return new ResultResponse(true);
	}

}
